﻿using AbpApplication.Expand;
using AbpApplication.InPut;
using AbpToDomain.EntityInfo;
using Castle.Core.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Logging;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace AbpApplication.AppServices
{
    public class EssService : ApplicationService, IEssService
    {
        private readonly IRepository<TaskOrder> TaskOrder_res;
        private readonly IRepository<Box> box_res;
        private readonly IRepository<WarehouseLine> warehouse_line_res;
        private readonly IRepository<SowingWallLine> sowingwallLine_res;
        private readonly ILoginHelper loginHelper;
        private readonly IBaseService baseService;

        public EssService(IRepository<TaskOrder> taskOrder_res, IRepository<Box> box_res, IRepository<WarehouseLine> warehouse_line_res, ILoginHelper loginHelper, IBaseService baseService, IRepository<SowingWallLine> sowingwallLine_res)
        {
            TaskOrder_res = taskOrder_res;
            this.box_res = box_res;
            this.warehouse_line_res = warehouse_line_res;
            this.loginHelper = loginHelper;
            this.baseService = baseService;
            this.sowingwallLine_res = sowingwallLine_res;
        }
        public async Task<ApiResponse> AbnormalCapture(InPut_EssService2 InPut)
        {
            return new ApiResponse("ok", true, null);
        }//异常回调
        public async Task<ApiResponse> TaskStatusChange(InPut_EssService InPut)
        {
            try
            {   //任务状态上报        
                if (InPut.eventType == "task")
                {
                    if (InPut.status == "success")
                    {
                        TaskOrder taskOrder = await TaskOrder_res.FirstOrDefaultAsync(x => x.TaskOrderCode == InPut.taskCode);
                        if (taskOrder == null)
                        {
                            return new ApiResponse("未查询到该任务", false, null);
                        }
                        if (taskOrder.Type == "出库")
                        {
                            Box box = await box_res.FirstOrDefaultAsync(x => x.Id_Box == taskOrder.Id_Box);
                            WarehouseLine warehouseLine = await warehouse_line_res.FirstOrDefaultAsync(x => x.WarehouseLineCode == taskOrder.WarehouseCode);
                            warehouseLine.IsOccupy = false;
                            box.WarehouseLine = null;
                            box.Id_WarehouseLine = null;

                            //出库的是播种墙
                            if (taskOrder.InlethouseCode.Contains("CS"))
                            {
                               SowingWallLine sowingWallLine=  await sowingwallLine_res.FirstOrDefaultAsync(x=>x.SowingWallLine_Name==taskOrder.InlethouseCode);
                                if(sowingWallLine != null)
                                {
                                    sowingWallLine.IsOccupy = true;
                                    sowingWallLine.BoxCode=box.BoxCode;
                                    await sowingwallLine_res.UpdateAsync(sowingWallLine);
                                }
                            }
                       
                            await box_res.UpdateAsync(box);
                            await warehouse_line_res.UpdateAsync(warehouseLine);
                            await TaskOrder_res.DeleteAsync(taskOrder);
                            return new ApiResponse("状态修改成功", true, null);
                        }
                        if (taskOrder.Type == "入库")
                        {
                            Box box = await box_res.FirstOrDefaultAsync(x => x.Id_Box == taskOrder.Id_Box);
                            WarehouseLine warehouseLine= await warehouse_line_res.FirstOrDefaultAsync(x => x.WarehouseLineCode == taskOrder.WarehouseCode);
                            box.Id_WarehouseLine = warehouseLine.Id_WarehouseLine;
                            box.WarehouseLine = warehouseLine;
                            await box_res.UpdateAsync(box);
                            await warehouse_line_res.UpdateAsync(warehouseLine);
                            await TaskOrder_res.DeleteAsync(taskOrder);
                            return new ApiResponse("状态修改成功", true, null);
                        }
                    }
                    if (InPut.status == "cancel")
                    {
                        TaskOrder taskOrder = await TaskOrder_res.FirstOrDefaultAsync(x => x.TaskOrderCode == InPut.taskCode);
                        if (taskOrder == null)
                        {
                            return new ApiResponse("未查询到该任务", false, null);
                        }
                        if (taskOrder.Type == "入库")
                        {
                            WarehouseLine warehouseLine = await warehouse_line_res.FirstOrDefaultAsync(x => x.WarehouseLineCode == taskOrder.WarehouseCode);
                            warehouseLine.IsOccupy = false;
                            await warehouse_line_res.UpdateAsync(warehouseLine);
                            await TaskOrder_res.DeleteAsync(taskOrder);
                            loginHelper.LoginAsync(InPut, $"TaskStatusChange-任务取消了+{taskOrder.Id_Box + taskOrder.WarehouseCode + taskOrder.TaskOrderCode}");
                            return new ApiResponse("任务取消成功", true, null);
                        }
                        if (taskOrder.Type == "出库")
                        {
                            if (taskOrder.OrderNo == "pda")
                            {
                               SowingWallLine sowingWallLine=  await sowingwallLine_res.FirstOrDefaultAsync(x=>x.SowingWallLine_Name==taskOrder.InlethouseCode);
                                if(sowingWallLine!=null)
                                {
                                    sowingWallLine.IsOccupy=false;
                                    await sowingwallLine_res.UpdateAsync(sowingWallLine);
                                }
                            }

                            await TaskOrder_res.DeleteAsync(taskOrder);
                            loginHelper.LoginAsync(InPut, $"TaskStatusChange-任务取消了+{taskOrder.Id_Box + taskOrder.WarehouseCode + taskOrder.TaskOrderCode}");
                            return new ApiResponse("任务取消成功", true, null);
                        }
                    }
                }
                //取箱状态上报
                if (InPut.eventType == "tote_load")
                {
                    if (InPut.status == "success")
                    {
                        TaskOrder taskOrder = await TaskOrder_res.FirstOrDefaultAsync(x => x.TaskOrderCode == InPut.taskCode);
                        if (taskOrder == null)
                        {
                            return new ApiResponse("未查询到该任务", false, null);
                        }
                        if (taskOrder.Type == "入库")
                        {
                            SowingWallLine sowingWallLine = await sowingwallLine_res.FirstOrDefaultAsync(x => x.SowingWallLine_Name == taskOrder.InlethouseCode);
                            if (sowingWallLine != null)
                            {
                                sowingWallLine.IsOccupy = false;
                                sowingWallLine.BoxCode = null;
                                await sowingwallLine_res.UpdateAsync(sowingWallLine);
                            }
                        }
           

                    }
                }
                return new ApiResponse("ok", true, null);
            }
            catch (Exception ex)
            {
                InPut_BaseService15 inPut = new InPut_BaseService15();
                inPut.User = "ESS";
                inPut.Note = InPut.message+InPut.eventType+InPut.containerCode+InPut.isLocationHasContainer+InPut.stationCode+InPut.status;
                inPut.Type = "异常";
                await baseService.CreateLog(inPut);
                loginHelper.LoginAsync(InPut, "TaskStatusChange-任务失败");
                return new ApiResponse("内部错误服务器", false, null);
            }
           
        }//任务状态
    }
}
